<?php
require_once('classes/models/user.php');
require_once('classes/managers/user_manager.php');
class UserController extends Controller
{
	public function index()
	{
		$content = new View('views/user/index.php');
		$manager = new UserManager();
		$content->users = $manager->get_all();
		
		$this->template->title = "User Index";
		$this->template->content = $content;
		echo $this->template;
	}
	
	public function login()
	{
		$content = new View('views/user/login.php');
		
		$this->template->title = "Login";
		$this->template->content = $content;
		echo $this->template;	
	}
	
	public function auth()
	{
		set_global_default('user_name', '');
		set_global_default('password', '');
		
		$manager=new UserManager();
		if ($user=$manager->auth($_POST['user_name'], $_POST['password']))
		{
			$_SESSION['user_id'] = $user->id;
			redirect('index.php');
		}
		else
		{
			$_SESSION['user_id'] = 0;
			$content = new View('views/user/login.php');

			$this->template->error_messages[] = 'Invalid Login';
			$this->template->title = "Login";
			$this->template->content = $content;
			echo $this->template;
		}
	}
	
	public function logout()
	{
		$_SESSION['user_id'] = 0;
		redirect('index.php');
	}
	
	public function edit()
	{
		set_global_default('id', 0);
		$content = new View('views/user/edit.php');
		
		$content->user = new User($_GET['id']);
		
		$this->template->title = "Edit User";
		$this->template->content = $content;
		echo $this->template;
	}
	
	public function detail()
	{
		set_global_default('id', 0);
		$content = new View('views/user/detail.php');
		
		$content->user = new User($_GET['id']);
		
		$this->template->title = "User Detail";
		$this->template->content = $content;
		echo $this->template;
	}
	
	public function save()
	{
		$user=new User($_POST['id']);
		
		if (isset($_POST['password'])) $_POST['password'] = md5($_POST['password']);
		if (isset($_POST['password_check'])) $_POST['password_check'] = md5($_POST['password_check']);

		$user->set_values();
		$errors = $this->validate_edit_user($user);
		if(count($errors) == 0)
		{
			$user->save();
			$_SESSION['user_id'] = $user->id;
			redirect('index.php');
		}
		else
		{
			$this->template->error_messages = $errors;
			$content = new View('views/user/edit.php');
			$content->user = $user;
			$this->template->content = $content;
			echo $this->template;
		}
	}
	
	public function delete()
	{
		set_global_default('id', 0);
		$user = new User($_POST['id']);
		$user->delete();
		
		redirect('index.php');
	}
	
	public function change_password()
	{
		$content = new View('views/user/change_password.php');
		
		$user = current_user();
		if ($user->is_new())
		{
			redirect('index.php');
		}
		$content->user = $user;
		$this->template->title = "Edit User";
		$this->template->content = $content;
		echo $this->template;
	}
	
	private function validate_edit_user($user)
	{
		$errors = array();
		if ($user->is_new())
		{
			// username and password are required;
			if (!$user->user_name) $errors[] = "User Name is Required.";
			if (!$user->password) $errors[] = "Password is Required";
			
			// username must be unique
			if (!$user->has_unique_name()) $errors[] = "User Name already taken";
			
			// passwords must match
			if (!$user->password == $user->password_check) $errors[] = "Passwords don't match!";
		}
		return $errors;
	}
	
	public function save_change_password()
	{
		$user = current_user();
		$manager=new UserManager();
		$errors=array();
		if (!$manager->auth($user->user_name, $_POST['old_password'])) $errors[] = "Wrong old password!";
		if ($_POST['new_password'] != $_POST['new_password_check'] ) $errors[] = "New Passwords don't match!";
		
		if (count($errors) == 0)
		{
			$user->password = md5($_POST['new_password']);
			$user->save();
			$this->template->content = "Password Changed! <a href='index.php'>Back to calendar</a>";
			echo $this->template;
		}
		else
		{
			$this->template->error_messages = $errors;
			$content = new View('views/user/change_password.php');
			$content->user = $user;
			$this->template->title = "Edit User";
			$this->template->content = $content;
			echo $this->template;
		}
	}
}
?>